package cn.zhqr.core.service;

import cn.zhqr.common.bean.response.ErrorCode;
import cn.zhqr.common.bean.response.OutResponse;
import cn.zhqr.db.model.AgentFrozenTrade;
import cn.zhqr.db.model.AgentWallet;
import com.jfinal.ext.plugin.tx.DbTx;
import com.jfinal.ext.plugin.tx.Tx;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

public class AgentFrozenTradeService {
    public static final AgentFrozenTradeService instance = new AgentFrozenTradeService();
    public static final Logger logger = Logger.getLogger(AgentFrozenTradeService.class);

    /**
     * 冻结
     * @param agentId
     * @param freezeAmount      冻结金额
     * @param relationNo        关联单号
     */
    public OutResponse freeze(Long agentId,BigDecimal freezeAmount,int frozenType,BigInteger relationNo){
        OutResponse[] result = new OutResponse[1];
        DbTx.execute(new Tx() {
            @Override
            public void sql() {
                AgentWallet.dao.increaseFrozenBalance(agentId,freezeAmount);

                AgentFrozenTrade frozenTrade = new AgentFrozenTrade();
                frozenTrade.setType(frozenType);
                frozenTrade.setAmount(freezeAmount);
                frozenTrade.setAgentId(agentId);
                frozenTrade.setRelationNo(relationNo);
                frozenTrade.setCreateTime(new Date());
                frozenTrade.save();

                result[0] = OutResponse.buildSuccess();
            }

            @Override
            public void error(Exception e) {
                logger.error("agent freeze transaction error:",e);
                result[0] = OutResponse.buildError(ErrorCode.SQL_ERROR);
            }
        });
        return result[0];
    }

    /**
     * 解冻
//     * @param agentId
//     * @param unfreezeAmount    解冻金额
     * @param relationNo        关联单号
     */
    public OutResponse unfreeze(BigInteger relationNo){
        OutResponse[] result = new OutResponse[1];
        DbTx.execute(new Tx() {
            @Override
            public void sql() {
                List<AgentFrozenTrade> agentFrozenTradeList = AgentFrozenTrade.dao.findFreezeByRelationNo(relationNo);
                for (AgentFrozenTrade trade : agentFrozenTradeList){
                    //冻结解冻，总是成对出现。需要有冻结操作，才能有对应的解冻操作。
                    AgentWallet.dao.increaseFrozenBalance(trade.getAgentId(),trade.getAmount().negate());

                    AgentFrozenTrade frozenTrade = new AgentFrozenTrade();
                    frozenTrade.setType(trade.getType()+1000);
                    frozenTrade.setAmount(trade.getAmount());
                    frozenTrade.setAgentId(trade.getAgentId());
                    frozenTrade.setRelationNo(relationNo);
                    frozenTrade.setCreateTime(new Date());
                    frozenTrade.save();
                }

                result[0] = OutResponse.buildSuccess();
            }

            @Override
            public void error(Exception e) {
                logger.error("agent unfreeze transaction error:",e);
                result[0] = OutResponse.buildError(ErrorCode.SQL_ERROR);
            }
        });

        return result[0];
    }
}
